;#IfWinActive, Atlantica Online
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetMouseDelay,-1
#SingleInstance
#include Lib\MemoryFunctions.ahk
#include Lib\ImageFunctions.ahk
#include config.ahk
;#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

	Rezz() {
		
		GlobalVars()
		static maxmp:=Object()
		static mp:=Object()
		mpdeficittotal=0
		rezz=0
		
		food=0
		loop, 9 {
		mp[A_Index]:=getMP(A_Index)
		hp:=getHP(A_Index)
		if (mp[A_Index]>maxmp[A_Index]) {
		maxmp[A_Index]:=mp[A_Index]
			}	
		if (hp=0 && maxmp[A_Index]>0) {
			rezz++
		}
	
		mpdeficittotal+=(maxmp[A_Index]-mp[A_Index])
	}
	
	;food:=mpdeficittotal//foodmpreturned
	Global mpthreshold
	tooltip, dead %rezz% mpdeficit %mpdeficittotal% mpthreshold %mpthreshold% ,450,0,5
	
	
	if (rezz>0 || (mpdeficittotal>mpthreshold)) {
		WinGetActiveStats, Title, ScreenWidth, ScreenHeight, posix, posiy
		send {Space}
		sleep, 200
		if (rezz>0) {
			lifepotionx=0
			revivex=0
			now:=A_now
			elapsed:=0
			while (lifepotionx=0 && elapsed<2) {
				EnvSub, elapsed, now, seconds
				ImageSearch, lifepotionx, lifepotiony, 0, 0, ScreenWidth, ScreenHeight, *30 Images\Items\lifepotion.png 
				if (lifepotionx>0) {
					MouseClick, right, lifepotionx, lifepotiony
				}
				else {
					ImageSearch, lifepotionx, lifepotiony, 0, 0, Screenwidth, Screenheight, *30 Images\Items\lifepotion2.png 
					if (lifepotionx>0) {
					MouseClick, right, lifepotionx, lifepotiony
					}
				}
				sleep, 100
				
			}
			sleep, 100
		}
		now:=A_now
		elapsed:=0
		while (mpdeficittotal>mpthreshold && elapsed<2) {
			EnvSub, elapsed, now, seconds
			ImageSearch, mpfoodx, mpfoody, 0, 0, 1600, 1000, *70 Images\Items\carpa.png

			if (ErrorLevel=0) {
				MouseClick, right, mpfoodx, mpfoody, 2
				mpdeficittotal-=3450
			}
			else {
				ImageSearch, mpfoodx, mpfoody, 0, 0, 1600, 1000, *70 Images\Items\carneiro.png
				if (ErrorLevel=0) {
					MouseClick, right, mpfoodx, mpfoody, 2
					mpdeficittotal-=2300
				}
				else {
					ImageSearch, mpfoodx, mpfoody, 0, 0, 1600, 1000, *70 Images\Items\porcogrelhado.png
					if (ErrorLevel=0) {
						MouseClick, right, mpfoodx, mpfoody, 2
						mpdeficittotal-=650
					}
					else {
						ImageSearch, mpfoodx, mpfoody, 0, 0, 1600, 1000, *70 Images\Items\seafood.png
						if (ErrorLevel=0) {
							MouseClick, right, mpfoodx, mpfoody, 2
							mpdeficittotal-=7000
						}
					}
				}
			}
			food-=1
			if (food>0) {
				sleep 500
			}
		}
		send {Space}
		if (rezz>0)	{
			now:=A_now
			elapsed:=0
			while (elapsed<2){
				closeShit()
				sleep, 100
				ImageSearch, revivex, revivey, 250, 250, ScreenWidth-300, ScreenHeight-200, *30 Images\Misc\reviver.png
				ImageSearch, revivetodosx, revivetodosy, 250, 250, ScreenWidth-300, ScreenHeight-200, *30 Images\Misc\revivertodos.png
				EnvSub, elapsed, now, seconds
				if (revivex>0) { 
					MouseClick, left, revivex, revivey
					now:=A_now
					sleep, 500
				}
				if (revivetodosx>0){ 
					MouseClick, left, (revivetodosx+139), (revivetodosy-154)
					now:=A_now
					sleep, 500
				}
			}
		}
	}
}


Rawr(){
	GlobalVars()
	
	
	Global Target
	Global ABTimer
	
	Rezz()
	MapChars()
	NextTarget()
	Init()
	
	while (1=1) {
		MapClick()
		AutoBattle()
		Init()
		CloseShit()
		acceptInfo()
		Rezz()
		;useMPFood()
	}
}


Skirmish(){
	GlobalVars()
	Global Target
	Global ABTimer
	Global battlespeed
	SetSpeed(battlespeed)
	Rezz()
	NextTarget()
	while (1=1) {
		
		MapClick()
		
		AutoBattleSkirmish()
		CloseShit()
		Rezz()
		MouseClick, left, 582, 498
		MouseClick, left, 582, 498
	}
}


AutoBattleSkirmish() {
	while (inBattle()) {
		    ifWinExist , Sponsored session
    {
        WinActivate
        sleep, 500
        Send , {Enter}
    }
		WinActivate, Atlantica Online
		if (getActiveChar()=1) {
			if (isABZero()) {
				Send, f
			}
			else {
				EnableABSkirmish()
			}
		}
		sleep 1000
	}
}
			
		

AutoBattle() {
	
	waitForTurn()
	disableAB()
	minimizeExpGanha()
	Global ABtimer
	global stuckLimit
	global skipturn
	static mob:=0

	SetBattleZoom(50.0)
	CloseInventory()
	elapsed:=0
	startTime:=A_Now
	elapsed:=0
	stuckTimer:=0
	stuck:=0
	ab:=0
	mob:=0
	while (inBattle()){
		
		WinActivate, Atlantica Online
		
		closeShit()
		
		if (!stuck) {
			stuckStart:=A_Now
		}
		
		EnvSub, elapsed, startTime, seconds
		EnvSub, stuckTimer, stuckStart, seconds
		if (elapsed) {
			tooltip Elapsed:%elapsed%, 300, 0, 8 
		}
		if (stuckTimer) {
			tooltip Stuck:%stuckTimer% , 375 ,0,4
		}
		
		if ((elapsed>ABtimer)&&(ab=0)){
			ab:=1
		}
		if ((stuckTimer>stuckLimit)&&(ab=0)){
			ab:=1
		}
		if (ab>0) {
				enableAB()
				sleep 2000
				ab:=2
		}
		if (ab=0) {
			t:=getTurn()
			if (t=1) {
				stuck:=1
				continue
			}
			if (mod(t,2)) {
				stuck:=0
				continue
			}
			if (ReadCursor()!=1||mob<1) {
				mob:=findLivingMob()
			}
			ToolTip Mob: %mob%,  0 , 0 , 1 
			
			if (t=skipturn) {
					send, x
					sleep, 200
					send, ````
					
				}
			
			if (mob>0) {
	
				merc:=returnActiveMerc()
				x:=targetX(mob)
				y:=targetY(mob)
				ac:=getAction(merc,t)
				ToolTip M:%merc% T: %t% A: %ac%, 100 , 0 , 2

				if (ac="skip") {
					Send, f
				}
				
				else if (ac="guard") {
					Send, g
				}
				else if (ac="cast") {
					if (isSpellAvail(merc)) {
						MouseClick, Right, x, y
						;msgbox, casting
					}
					else {
						MouseClick, left, x, y
					}
				}
				else if (ac="heal") {
					if (isSpellAvail(merc)) {
						Loop 9 {
							hp:=getHP(A_Index)

							if (hp>0) {
								pcthp:=hp/max_hp_%A_Index%
								if (pcthp<0.6) {
									pos:=pos_%A_Index%
									MouseMove, xm_%pos%, ym_%pos%
									Sleep 100
									MouseClick, Right, xm_%pos%, ym_%pos%
									break
								}
							}
							if (A_Index=9) {
								Send, g
							}
						}
					}
					else {
						Send, g
					}
				}
				else if (ac="awaken") {
					if (isSpellAvail(merc)) {
						Loop 9 {
							combo:=getCombo(A_Index)
							if (combo>0) {
								pos:=pos_%A_Index%
								if (pos=1||pos=2||pos3||pos5) {
									pos:=2
								}
								else if (pos=4||pos=6||pos8) {
									pos:=5
								}
								else if (pos=7) {
									pos:=4
								}
								else if (pos=9) {
									pos:=6
								}
								MouseMove, xm_%pos%, ym_%pos%
								Sleep 100
								MouseClick, Right, xm_%pos%, ym_%pos%
							}
							else if (A_Index=9) {
								Send, g
							}
						}
					}
					else {
						Send, g
					}
				}
				else if (ac="attack") {
					MouseClick, left, x, y
				}
				mercAtual:=merc
				now2:=A_now
				elapsed2:=0
				Loop{
					EnvSub, elapsed2, now2, seconds
					sleep, 5
					if (elapsed2>0) {
						stuck:=1
						break
					}
					merc:=returnActiveMerc()
					if (merc!=mercAtual) {
						stuck:=0
						break
					}
				}
			}
			else if (mob=-1) {
				stuck:=1
				Send, x
				sleep, 100
			}
			else if (mob=-2) {
				stuck:=1
				if (inBattle()) {
					Send, x
				}
				MouseMove, 200, 200
				sleep, 100
			}
		}
	} 
}

MapClick(){
	Global Target
	Global automapid
	Global autox
	Global autoy
	notFound:=0
	Init()
	fecharGD()
	MouseClick, left, 594, 338
	c:=0
	while (inBattle()=0){
		closeShit()
		amIDead(automapid, autox, autoy)
		
		    ifWinExist , Sponsored session
    {
        WinActivate
        sleep, 500
        Send , {Enter}
    }
		WinActivate, "Atlantica Online"
		stuck:=0
		ImageSearch, targetx, targety, 500, 200, 1050, 775, *50 %target%
		if (ErrorLevel=2){
		msgbox, err %err% target %target%
		}	
		
		if (ErrorLevel=1) {
			ImageSearch, targetx, targety, 100, 200, 1050, 500, *70 %target%
		}
		if (ErrorLevel=0) {
			MouseMove, targetx, targety
			sleep, 50
			if (ReadCursor()=1){
				MouseClick, left
				stuck:=0
				while (!stuck) {
					x0:=GetX()
					y0:=GetY()
					Tooltip Pos: %x0% %y0%`nT: %target% , 775,0,6 
					Sleep 600
					x:=GetX()
					y:=GetY()		
					dx:=abs(x0-x)
					dy:=abs(y0-y)
					
					if (inBattle()) {
						break
					}
					if (dx=0&&dy=0) {
						stuck:=1
					}
				}	
				notFound:=0
			}
			else {
				notFound++
			}
		}
		if (ErrorLevel=1) { ; n acho mob
			notFound++
		}
		if ((NotFound>10)||(stuck=1)) {
			SpinCam()
			sleep, 100
			notFound:=0
		}
		sleep, 100
	}
}

SpinCam(){
		Random, c, 2, 4
		SetCamera(c)
		Send {s down}
		sleep 50
		Send {s up}
		sleep 200
		Init()
		return c
	}

Init() {
	Global battlespeed
	SetBattleZoom(50.0)
	SetCamera(0.0)
	SetSpeed(battlespeed)
}

AltJ(){
	if (WinActive("Atlantica")) {
	send, !J
	}	
}

AltR(){
	if (WinActive("Atlantica")) {
	send, !R
	}
}

AutoRun(){
	send, {NumLock}
	;SetNumlockState, on
}

spamClick(){
	SetMouseDelay, -1
     While 1=1{
		MouseClick, right
	}
}
	
	
	
turnShitIn(){
	SetMouseDelay, -1

	While 1=1{
		MouseClick, left, 192, 460
		MouseClick, left, 223, 633
		MouseClick, left, 523, 621
	}
}

amITooFar(map, autox, autoy) {
		x:=GetX()
		y:=GetY()		
		dx:=abs(autox-x)
		dy:=abs(autoy-y)
		dist:=sqrt(dx**2+dy**2)
		if (dist>20.0) {

			AutoMove(autox,autoy,map)
			sleep 1000
			MouseClick, left, 594, 338
		}
}
		
	
